﻿using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfBehaviorDemo.Behaviors
{
    public class DoDropBehavor : Behavior<ListBox>
    {
        /// <summary>
        /// 当行为被附加到 ListBox 时触发。
        /// </summary>
        protected override void OnAttached()
        {
            this.AssociatedObject.MouseMove += AssociatedObject_MouseMove;
            base.OnAttached();
        }

        private void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                var selectItem = this.AssociatedObject.SelectedItem;//获取选中元素

                DragDrop.DoDragDrop(this.AssociatedObject, selectItem, DragDropEffects.Copy);
            }
        }


        /// <summary>
        /// 当行为从控件移除时触发。
        /// </summary>
        protected override void OnDetaching()
        {
            this.AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
            base.OnDetaching();
        }
    }
}
